package ObjectMethodTest;
/*
重写equals方法
    需求：当一个学生的学号相等，并且学校相同时，表示同一个学生
 */
public class EqualsTest3 {
    public static void main(String[] args){
        Student s1 = new Student(12,"哈喽中学");
        Student s2 = new Student(12,"哈喽中学");
        System.out.println(s1.equals(s2)); //true
    }
}
class Student{
    int no;
    String school;
    public Student(){

    }
    public Student(int no, String school){
        this.no = no;
        this.school = school;
    }
    public String toString(){
        return "学号:"+this.no+" 学校:"+this.school;
    }
    public boolean equals(Object obj){
        if(obj == null || !(obj instanceof Student)){
            return false;
        }
        if(obj == this){
            return true;
        }
        Student stu = (Student)obj;
        if(this.no == stu.no && this.school.equals(stu.school)){
            return true;
        }
        return false;
    }
}
